import { Component, OnInit, Input } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { TitleService } from '@delon/theme'
import * as dayjs from 'dayjs'

import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { I18NService } from '@core/i18n/i18n.service'
import { PageService } from '@shared/services/page.service'
import { IHoldReleaseDetail, EHoldReason } from '@shared/interfaces/operation'

@Component({
  selector: 'app-hold-detail',
  templateUrl: './hold-detail.component.html',
})
export class HoldDetailComponent extends DetailBaseClass<IHoldReleaseDetail> implements OnInit {
  @Input() holdDetail: IHoldReleaseDetail
  firstColumns: IBaseInfoColumn[]
  secondColumns: IBaseInfoColumn[]

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public pageService: PageService,
  ) {
    super(pageService)
  }

  ngOnInit() {
    this.detailData = this.holdDetail
    this.initFirstColumns()
    this.initSecondColumns()
    this.initColumns()
  }

  initFirstColumns(): void {
    this.firstColumns = [
      {
        span: 6,
        index: 'riskTime',
        label: '操作时间',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.RISKTIME',
        type: 'date',
      },
      {
        span: 6,
        label: '已暂扣时间(天)',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.HOLDEDDAYS',
        displayValue: this.getHoldDays(),
      },
      {
        span: 6,
        index: 'reason',
        label: '暂扣原因',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.HOLD_REASON',
        pipe: this.getReason.bind(this),
      },
      {
        span: 6,
        index: 'operator',
        label: '操作用户',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.OPERATOR',
      },
    ]
  }

  initSecondColumns(): void {
    this.secondColumns = [
      {
        span: 24,
        label: '备注',
        labelI18n: 'GENERAL.INFO.REMARK',
        index: 'remark',
      }
    ]
  }

  getHoldDays(): string {
    const { riskTime } = this.detailData
    const today = dayjs()
    const days = today.diff(dayjs(riskTime), 'day')
    return days === 0 ? '<1' : days.toString()
  }

  getReason(reason: EHoldReason): string {
    return this.i18n.fanyi(EHoldReason[reason])
  }

}
